﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Drawing;
using System.IO;
using ComLib.ComType;

namespace CoreBroker
{
    /// <summary>
    /// 照片综合处理过程阶段方法
    /// </summary>
    public enum DetectProcessMethods : int
    {
        DetectEye = 0,                  // 只检测眼睛
        DetectFront = 1,               // 只检测正脸，包含眼睛和姿态
        Detect = 2,                       // 执行所有检测选项
        DetectAndTake = 3,                 // 执行所有检测选项和处理流程
        ExitApp = 4,                            // 关闭应用程序
    }

    /// <summary>
    /// 用户照片集合，源文件和最终可用文件
    /// </summary>
    public class TakenPhoto : ICloneable, IDisposable
    {

        public int Index = 0;

        /// <summary>
        /// 原始图像
        /// </summary>
        //public Bitmap SourceImage = null;

        /// <summary>
        /// 最终图像
        /// </summary>
        //public Bitmap DestImage = null;


        private FaceColor _lab = new FaceColor();
        public FaceColor LAB
        {
            get
            {
                return _lab;
            }
            set
            {
                _lab = value;
            }
        }

        public string SourceImagePath = "";

        public string DestImagePath = "";

        /// <summary>
        /// 检测不合格的原因
        /// 如果为空，表示检测合格
        /// </summary>
        public string NotPassReason 
        { 
            get; 
            set; 
        }

        /// <summary>
        /// 指示一所质检是否检测合格
        /// </summary>
        public bool QCPass
        {
            get;
            set;
        } = false;

        public TakenPhoto(int index, string src, string dst, bool pass=false)
        {
            SourceImagePath = src;
            DestImagePath = dst;
            Index = index;
            QCPass = pass;
            //LoadBitmap();
        }

        public TakenPhoto()
        {

        }

        //private void LoadBitmap()
        //{
        //    try
        //    {
        //        if (File.Exists(SourceImagePath))
        //        {
        //            System.IO.FileStream fs = new System.IO.FileStream(SourceImagePath, FileMode.Open, FileAccess.Read);
        //            SourceImage = new Bitmap(System.Drawing.Image.FromStream(fs));
        //            fs.Close();
        //        }
        //        if (File.Exists(DestImagePath))
        //        {
        //            System.IO.FileStream fs = new System.IO.FileStream(DestImagePath, FileMode.Open, FileAccess.Read);
        //            DestImage = new Bitmap(System.Drawing.Image.FromStream(fs));
        //            fs.Close();
        //        }
        //    }
        //    catch
        //    {
        //        throw;
        //    }   
        //}

        public object Clone()
        {
            TakenPhoto clone = new TakenPhoto();
            //if (SourceImage != null)
            //{
            //    clone.SourceImage = SourceImage.Clone() as Bitmap;
            //}
            //if (DestImage != null)
            //{
            //    clone.DestImage = DestImage.Clone() as Bitmap;
            //}
            clone.Index = Index;
            clone._lab = this._lab;
            clone.SourceImagePath = SourceImagePath;
            clone.DestImagePath = DestImagePath;
            return clone;
        }

        public void Dispose()
        {
            SourceImagePath = "";
            DestImagePath = "";
            //if (SourceImage != null)
            //{
            //    SourceImage.Dispose();
            //    SourceImage = null;
            //}
            //if (DestImage != null)
            //{
            //    DestImage.Dispose();
            //    DestImage = null;
            //}
        }
    }

    /// <summary>
    /// 检测请求参数
    /// </summary>
    [DataContract]
    public class DetectRequestArgs
    {
        /// <summary>
        /// 请求照片的检测类型
        /// </summary>
        [DataMember]
        public DetectProcessMethods Method { get; set; }

        /// <summary>
        /// 照片保存名称(基础名称)
        /// </summary>
        [DataMember]
        public string PhotoName { get; set; }

        /// <summary>
        /// 当前请求的序号(标识流程当前所在步骤)
        /// </summary>
        [DataMember]
        public int Index { get; set; }

        /// <summary>
        /// 照片业务应用类型
        /// </summary>
        [DataMember]
        public ApplyTo App { get; set; }
    }

    /// <summary>
    /// 检测结果参数
    /// </summary>
    [DataContract]
    public class DetectResultArgs
    {
        /// <summary>
        /// 照片的检测类型
        /// </summary>
        [DataMember]
        public DetectProcessMethods Method { get; set; }

        /// <summary>
        /// 照片业务应用类型
        /// </summary>
        [DataMember]
        public ApplyTo App { get; set; }

        /// <summary>
        /// 当前请求的序号(标识流程当前所在步骤)
        /// </summary>
        [DataMember]
        public int Index { get; set; }

        /// <summary>
        /// 照片源文件名称
        /// </summary>
        [DataMember]
        public string SourceFileName { get; set; }

        /// <summary>
        /// 照片目标保存名称
        /// </summary>
        [DataMember]
        public string DestFileName { get; set; }

        /// <summary>
        /// 检测执行结果
        /// </summary>
        [DataMember]
        public bool Success { get; set; }

        /// <summary>
        /// 检测过程中发生的错误
        /// </summary>
        [DataMember]
        public string ErrorMessage { get; set; }

        [DataMember]
        public RecogOut Result
        {
            get;
            set;
        }
    }
}
